/*	Copyright 2008 Jacquemin Valentin

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
	
	@author pollux
 */
package org.poxd.ui;

import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionListener;

import org.apache.log4j.Logger;
import org.poxd.model.Category;
import org.poxd.model.Language;
import org.poxd.model.LanguageModel;
import org.poxd.persistance.LanguageDAO;

/**
 * Language List controller
 * 
 * @author pollux
 * 
 */
public class LanguagesPanel extends JPanel {
	private class DeleteKeyListener extends KeyAdapter {
		public void keyPressed(KeyEvent e) {
			if (e.getKeyChar() == KeyEvent.VK_DELETE) {
				final Language selected = (Language) panel.getSelectedValue();
				final int[] index = { panel.getSelectedIndex() };
				e.consume();
				new Thread(new Runnable() {
					public void run() {
						if (index[0] == model.getSize()-1) {
							index[0] = index[0] - 1;
						}
						model.removeLanguage(selected);
						SwingUtilities.invokeLater(new Runnable() {
							public void run() {
								panel.setSelectedIndex(index[0]);
								controller.setLanguage(model
										.getElementAt(index[0]));
							}
						});
					}
				}, "DELETE LANGUAGE THREAD").start();
			}
		}
	}
	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(LanguagesPanel.class);
	private List<Language> languages;
	private final JList panel;
	private MainPane controller;

	private LanguageModel<Language> model;
	
	public LanguagesPanel(final MainPane controller) {
		this.controller = controller;
		setBackground(Color.WHITE);

		LanguageDAO l = new LanguageDAO();
		languages = l.listContain();
		log.debug(Arrays.toString(languages.toArray()));

		model = new LanguageModel<Language>(languages);
		panel = new JList(model);
		panel.addKeyListener(new DeleteKeyListener());
		panel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		panel.setFixedCellHeight(55);
		panel.setFixedCellWidth(40);
		panel.setBackground(Color.WHITE);
		panel.setMaximumSize(panel.getMinimumSize());
		panel.setCellRenderer(new LanguageCellRenderer());
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		JScrollPane scroll = new JScrollPane(panel);
		scroll.setBorder(null);
		add(scroll);
		add(Box.createVerticalGlue());
	}
	
	public void addCategory(Category newCat) {
		getSelectedLanguage().getCategories().put(newCat.getName(), newCat);
	}
	
	public void addLanguage(Language l) {
		model.addLanguage(l);
		panel.setSelectedValue(l, true);
	}

	public void addListSelectionListener(ListSelectionListener l){
		panel.addListSelectionListener(l);
	}

	public List<Language> getModel() {
		return languages;
	}
	
	public int getSelectedIndex() {
		return panel.getSelectedIndex();
	}

	public Language getSelectedLanguage() {
		return model.getElementAt(panel.getSelectedIndex());
	}
	
	public void setSelectedLanguage()
	{
		//TODO
	}
}